import {
  queryPageDataReq,
  addItemReq,
  updateItemReq,
  deleteItemReq,
  toggleStateReq,
  queryItemInfoReq,
  getProxyScheduleListFun,
  studentListByCourseFun,
  getTradeHoursFun,
  saveOrderFun,
  getCardListByUserIdFun,
  getUserVipCardListFun,
} from "./services";
import { namespace } from "./configFile";
import { message } from "antd";

export default {
  namespace,
  state: {
    init: false,
    tradeHours: [],
    cardList: [],
    userInfo: {},
  },
  effects: {
    //课程列表
    *getProxyScheduleList({ params }, { call, put }) {
      const response = yield call(getProxyScheduleListFun, params);
      yield put({
        type: "save",
        payload: {
          ...response,
        },
      });
      return {
        success: true,
        data: response.data.records,
        total: response.data.total,
      };
    },
    //学员列表
    *getUserVipCardList({ params }, { call, put }) {
      const response = yield call(getUserVipCardListFun, params);
      // const response = yield call(studentListByCourseFun, params)
      yield put({
        type: "save",
        payload: {
          userInfo: response.data,
        },
      });
      return {
        success: true,
        data: [response.data],
      };
    },
    //学员会员卡
    *getCardListByUserId({ params }, { call, put }) {
      const response = yield call(getUserVipCardListFun, params);
      // const response = yield call(getCardListByUserIdFun, params)
      console.log(response);
      yield put({
        type: "save",
        payload: {
          userInfo: response.data,
        },
      });
    },
    //营业时间
    *getTradeHours({ params }, { call, put }) {
      const response = yield call(getTradeHoursFun, params);
      console.log("getTradeHours", response);
      yield put({
        type: "save",
        payload: {
          tradeHours: response.data,
        },
      });
    },
    //代客预约保存
    *saveOrder({ params, callback }, { call, put }) {
      const response = yield call(saveOrderFun, params);
      if (response.code === 200) {
        message.success(response.msg);
        callback && callback();
      } else {
        message.error(response.msg);
      }
    },
    *pageInit({ params }, { call, put }) {
      const res = yield call(queryPageDataReq, {
        ...params,
        courseType: 1,
      });
      yield put({
        type: "save",
        payload: {
          ...res,
        },
      });
    },
    *queryTable({ params }, { call, put }) {
      const res = yield call(queryPageDataReq, {
        ...params,
        courseType: 1,
      });
      yield put({
        type: "save",
        payload: {
          ...params,
          ...res,
        },
      });
      return {
        success: true,
        data: res,
        total: res.length,
      };
    },

    *submitItem({ params, callback }, { call }) {
      let res = null;
      if (params.id) {
        res = yield call(updateItemReq, params);
      } else {
        res = yield call(addItemReq, params);
      }
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *deleteItem({ params, callback }, { call }) {
      let res = yield call(deleteItemReq, params);
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *queryItemInfo({ params, callback }, { call }) {
      let res = yield call(queryItemInfoReq, params);
      if (res) {
        callback && callback(res);
      }
    },

    *toggleState({ params, callback }, { call }) {
      let res = yield call(toggleStateReq, params);
      if (res) {
        callback && callback();
      }
    },
  },
  reducers: {
    save(state: any, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
